package com.hashkey.wxbasemodule.manager;


import com.hashkey.wxbasemodule.base.BaseActivity;
import com.hashkey.wxbasemodule.utils.LogUtil;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public class ActivityManager {
    private final String TAG = this.getClass().getSimpleName();

    private ActivityManager() {}

    private static class SingleHolder {
        private static ActivityManager SINGLE = new ActivityManager();
    }

    public static ActivityManager getInstance() {
        return SingleHolder.SINGLE;
    }

    private List<WeakReference<BaseActivity>> activityList = new ArrayList<>();

    public int getActivitySize() {
        return activityList.size();
    }

    public void add(WeakReference<BaseActivity> weakRefActivity) {
        activityList.add(weakRefActivity);
    }

    public void remove(WeakReference<BaseActivity> weakRefActivity) {
        boolean removed = activityList.remove(weakRefActivity);

        LogUtil.d("remove activity reference " + removed);
    }

    public void finishAll() {
        for (WeakReference<BaseActivity> weakRefActivity: activityList) {
            if (weakRefActivity != null) {
                BaseActivity activity = weakRefActivity.get();

                if (activity != null && !activity.isFinishing()) {
                    activity.finish();
                }
            }
        }
    }

}
